home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11379 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  85 lines

  1. Path: ix.netcom.com!netnews
  2. From: jlilley@ix.netcom.com (John Lilley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: friend functions and access to private variables...
  5. Date: 14 Mar 1996 06:08:25 GMT
  6. Organization: Netcom
  7. Message-ID: <4i8d4p$57m@ixnews3.ix.netcom.com>
  8. References: <Dnyy92.MLL@news.uwindsor.ca>
  9. NNTP-Posting-Host: den-co20-12.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-NETCOM-Date: Wed Mar 13 10:08:25 PM PST 1996
  13. Keywords: friend
  14. X-Newsreader: WinVN 0.99.7
  15.  
  16. In article <Dnyy92.MLL@news.uwindsor.ca>, saed@engn.uwindsor.ca says...
  17. >
  18. >Dariusz Piatkowski asked:
  19. >>I'm dealing with two classes, where a member function of class A 
  20. >> is declared as a friend function of class B.
  21. >>Unfortunately this does not allow the friend function access to private >>variables 
  22. in class B...
  23.  
  24. >--- A friend method of a non friend class has no access to the private member data.
  25. >    Apparently the class to which that method belongs must be a friend. The friend
  26. >    declaration of that method is then redundant. 
  27. >
  28. >The code snippet below (compilable) will show what works and what doesn't.
  29. >
  30. >Aryan.
  31. >
  32. >//---- begin code
  33. >class X {
  34. >        friend void yMethod(X x);
  35. >        friend void aFunction(X x);
  36. >        friend class Z;
  37. >        private: int a;
  38. >        };
  39. >
  40. >void aFunction(X x){x.a=1;};
  41. >// okay: aFunction is friend of class X and may access private member 'a' of X
  42. >
  43. >class Z { public: void zMethod(X x){x.a=1;}; };
  44. >// okay: class Z is friend of X, zMethod may access private member 'a' of X
  45. >
  46. >class Y { public: void yMethod(X x){x.a=1;}; };
  47. >// error: member `a' is a private member of class `X' (class Y is not a friend)
  48. >// this is Dariusz's question
  49. >
  50. >void aFunction(X x, int i){ return x.a=1; };
  51. >// error: member `a' is a private member of class `X'
  52. >// (different signature, not a friend)
  53. >
  54.  
  55.  
  56.  
  57. This is not correct.  Class methods may be friends of classes without
  58. its class being a friend.  However, you must give the class scope of
  59. the method using '::', as follows:
  60.  
  61. class X;
  62.  
  63. class Y { public: void yMethod(X x); };
  64.  
  65. class X {
  66.         friend void Y::yMethod(X x);
  67.         friend void aFunction(X x);
  68.         friend class Z;
  69.         private: int a;
  70.         };
  71.  
  72. void Y::yMethod(X x) {x.a=1;}
  73. // okay:  Y::yMethod(X x) was declared a friend
  74.  
  75. void aFunction(X x){x.a=1;};
  76. // okay: aFunction is friend of class X and may access private member 'a' of X
  77.  
  78. class Z { public: void zMethod(X x){x.a=1;}; };
  79. // okay: class Z is friend of X, zMethod may access private member 'a' of X
  80.  
  81.  
  82. john lilley
  83. Nerds for Hire, Inc.
  84.  
  85.